from .DeviceABC import DeviceABC
from ..PSData import *

class DeviceManager(PSDataServer):
    def __init__(self) -> None:
        super().__init__()
        self._deviceTable = dict()          # 已经添加的设备列表
        self._deviceTypeTable = dict()      # 存放支持的设备类型，并用于构造设备
        self._setFcuntionTable = {
            'CreateDevice': self.CreateDevice,
        }

    def PsSetDataSync(self, data: 'PSData') -> 'bool':
        # return super().PsSetDataSync(data)
        if (not data.header in self._setFcuntionTable.keys()):
            return False
        return self._setFcuntionTable[data.header](data)

    def CreateDevice(self, data: 'PSData') -> 'bool':
        '''
        Function:
            创造一个 Device，并将此 Device 放在本列表中
        args:
            data.header = 'CreateDevice',
            data.args = {},
            data.value = {
                'name': str(Device的名称),
                'type': str(Device的类型),
                'config': str(Device的配置),
            },
        '''
        name_ = data.args.get('name', None)
        if (not isinstance(name_, str)):
            return False
        if (name_ in self._deviceTable.keys()):
            return False
        type_ = data.args.get('type', None):
        if (not isinstance(type_, str)):
            return False
        if (type_ not in self._deviceTypeTable.keys()):
            return False
        config_ = data.args.get('config', None)
        if (not isinstance(config_, dict)):
            return False
        device_ = self._deviceTypeTable[type_](name_)
        if (not isinstance(device_, DeviceABC)):
            return False
        device_.UpdateConfig(config_)
        self._deviceTable[name_] = device_
        return True

    

